package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LXW
 * Date: 2022-05-28
 * Time: 19:34
 */
public class DelOperation implements IOperation {
    @Override
    public void work(BookList books) {
        System.out.println("请输入你要删除的图书");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int size = books.getSize();
        int index = 0;
        int i = 0;
        for ( ;i < size; i++) {
            Book book = books.getBook(i);
            if(name.equals(book.getName())) {
                index = i;
                break;
            }
        }
        if(i > size) {
            System.out.println("没有你要删除的图书");
            return;
        }
        for (int j = index; j < size - 1 ; j--) {
            Book book = books.getBook(j + 1);
            books.setBook(j,book);
        }
        books.setBook(size,null);
        books.setSize(size--);
        System.out.println("删除成功");
    }
}
